package com.handpay.nocardpay.baselibrary.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.http.constant.HttpConstants;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.MacAddressUtils;

import java.util.Hashtable;
import java.util.Map;

import static com.handpay.nocardpay.baselibrary.http.constant.HttpConstants.HEADER_NAME_SESSIONID;


/**
 * @author sxshi on 2016/12/22.
 * @email:emotiona_xiaoshi@126.com
 * @describe:Describe the function  of the current class
 */

public class HttpHeadFactory {
    public static boolean isDoRSA = false;
    public static String keExchange = null;
    public static String sessionID;
    public static String onlineHostHeader = null;
    public static String seedstr = null;
    public static String rsts = null;

    public static Map<String, String> createRequestHeaders(Context context) {
        SecureManager.getInstance().CheckDesKey();
        Hashtable<String, String> headers = new Hashtable<String, String>();
        // http header里增加参数。
        String imei = MacAddressUtils.getIMEI(context);
        String mac = MacAddressUtils.getMacAddress(context);
        if (!TextUtils.isEmpty(imei)) {
            headers.put(HttpConstants.HEADER_NAME_IMEI, imei);
        }
        if (!TextUtils.isEmpty(mac)) {
            headers.put(HttpConstants.HEADER_NAME_MAC, mac);
        }
        String deviceUuid = ClientEngine.getInstance().getDeviceUuid();
        if (!TextUtils.isEmpty(deviceUuid)) {
            headers.put(HttpConstants.HEADER_NAME_HPTUDID, deviceUuid);
        }
        if (onlineHostHeader != null) {
            headers.put("X-Online-Host", onlineHostHeader);
        }
        if (sessionID != null) {
            headers.put(HEADER_NAME_SESSIONID, sessionID);
        }
        headers.put(HttpConstants.HEADER_NAME_Version, ClientEngine.getInstance().HPVIRSION);
        headers.put("Content-Type", "application/x-www-form-urlencoded");//   headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");HttpUrlconnection 会报500
        headers.put("Connection", "Keep-Alive");
        headers.put("X-HPDataType", "json");
        // terminalUserId和密钥交换没有关系
        String terminalu = ClientEngine.getInstance().getRMSData(HttpConstants.HEADER_NAME_TerminalUserId);
        if (terminalu != null) {
            headers.put(HttpConstants.HEADER_NAME_TerminalUserId, terminalu);
        }
        seedstr = SecureManager.getInstance().Is_NeedCreateSeed();
//        HPLog.i(HPLog.YL_TAG, "seedstr:" + seedstr);
        if (seedstr != null) {
            // 做密钥交换，默认交换是失败了
            isDoRSA = true;
            keExchange = null;
            byte[] data = null;
            try {
                data = seedstr.getBytes("UTF-8");
            } catch (Exception e) {
                data = seedstr.getBytes();
            }
            rsts = SecureManager.getInstance().rsaEnCrypt(data);
            rsts = "testrsaid|" + rsts;
            headers.put(HttpConstants.HEADER_NAME_KEYEXCHANGE, rsts);
        } else {
            isDoRSA = false;
        }
        return headers;
    }

    /**
     * 解析Http响应的Header
     *
     * @param headers
     * @return
     */
    public static Hashtable<String, String> createRspHeader(Map<String, String> headers) {
        Hashtable<String, String> headertable = new Hashtable<String, String>();
        if (headers != null) {
            for (String key : headers.keySet()) {
                String name = key;
                String value = headers.get(key);
                if (name.compareTo(HEADER_NAME_SESSIONID) == 0) {
                    sessionID = value;
                } else if (name.compareTo(HttpConstants.HEADER_NAME_TerminalUserId) == 0) {
                    ClientEngine.getInstance().saveRMSData(HttpConstants.HEADER_NAME_TerminalUserId, value);
                } else if (name.compareTo(HttpConstants.HEADER_NAME_KEYEXCHANGE) == 0) {
                    keExchange = value;
                }
                headertable.put(name, value);
            }
        }
        if (isDoRSA) {
            SecureManager.getInstance().ReSetSecure(keExchange);
        }
        return headertable;
    }
}
